﻿using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WPFStudy.PrismStudy.ViewModels
{
    /// <summary>
    /// 导航必须实现接口INavigationAware 或 IConfirmNavigationRequest
    /// </summary>
    public class NavigateAViewModel:BindableBase, IConfirmNavigationRequest
    {

        #region 标题

        /// <summary>
        /// 标题
        /// </summary>
        private string _title;

        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        #endregion 标题 

        /// <summary>
        /// 
        /// </summary>
        public NavigateAViewModel()
        {

        }

        #region 导航打开过程
        //1.调用 IsNavigationTarget() ：决定能否需要重用，可以重用，若当前对象已实例化，则不再进行构造；不能重用，将调用当前VM构造，进行实例
        //2.调用对应的 View；
        //3.调用当前 VM的构造；
        //4.调用 OnNavigatedTo()
        //5.离开当前导航前，触发 OnNavigatedFrom() / ConfirmNavigationRequest();
        #endregion
        /// <summary>
        /// 接收导航参数
        /// </summary>
        /// <param name="navigationContext"></param>
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            string title = navigationContext.Parameters.GetValue<string>("Title");
            Title = title;
        }

        /// <summary>
        /// 每次重新打开实例的时候，是否重用原来的实例；
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <exception cref="NotImplementedException"></exception>
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            //为true则不在重新调用=构造方法，为false将重新调用构造方法
            return false;
        }

        /// <summary>
        /// 拦截导航请求
        /// </summary>
        /// <param name="navigationContext"></param>
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        /// <summary>
        /// 为导航中涉及的对象提供一种方法，以确定导航是否请求应继续。
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <param name="continuationCallback"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
            bool result = true;
            if(MessageBox.Show("确认切换页面吗？","提示",MessageBoxButton.YesNo,MessageBoxImage.Question) != MessageBoxResult.Yes)
            {
                result = false;
            }
            continuationCallback(result);
        }
    }
}
